//
//  UIView+SJAdd.swift
//  SJKit
//
//  Created by 刘洋 on 2016/12/30.
//  Copyright © 2016年 刘洋. All rights reserved.
//

import UIKit

// MARK: - 布局

public extension SwordJoy where Base: UIView {
  public var origin: CGPoint {
    get { return base.frame.origin }
    set { base.frame.origin = newValue }
  }
  
  public var left: CGFloat {
    get { return base.frame.origin.x }
    set { base.frame.origin.x = newValue }
  }
  
  public var top: CGFloat {
    get { return base.frame.origin.y }
    set { base.frame.origin.y = newValue }
  }
  
  public var right: CGFloat {
    get { return base.frame.origin.x + base.frame.size.width }
    set { base.frame.origin.x = newValue - base.frame.size.width }
  }
  
  public var bottom: CGFloat {
    get { return base.frame.origin.y + base.frame.size.height }
    set { base.frame.origin.y = newValue - base.frame.size.height }
  }
  
  public var height: CGFloat {
    get { return base.frame.size.height }
    set { base.frame.size.height = newValue }
  }
  
  public var width: CGFloat {
    get { return base.frame.size.width }
    set { base.frame.size.width = newValue }
  }
  
  public var center: CGPoint {
    get { return base.center }
    set { base.center = newValue }
  }
  
  public var centerX: CGFloat {
    get { return base.center.x }
    set { base.center = CGPoint(x: newValue, y: base.center.y) }
  }
  
  public var centerY: CGFloat {
    get { return base.center.y }
    set { base.center = CGPoint(x: base.center.x, y: newValue) }
  }
  
  public var size: CGSize {
    get { return base.frame.size }
    set { base.frame.size = newValue }
  }
}

// MARK: - 截图

public extension SwordJoy where Base: UIView {
  /// 截图
  ///
  /// - Returns: 截出来的图
  public func snapshotImage(_ afterScreenUpdates: Bool = false) -> UIImage {
    UIGraphicsBeginImageContextWithOptions(base.bounds.size, base.isOpaque, 0)
    base.drawHierarchy(in: base.bounds, afterScreenUpdates: afterScreenUpdates)
    let snap = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return snap!
  }
    public func screenShot() -> UIImage {
        
        guard base.frame.size.height > 0 && base.frame.size.width > 0 else {
            
            return UIImage()
        }
        
        UIGraphicsBeginImageContextWithOptions(base.frame.size, false, 0)
        base.layer.render(in: UIGraphicsGetCurrentContext()!)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        
        return image ?? UIImage()
    }
   public func addSubViews(_ subViews: [UIView]) {
        for sub in subViews {
            base.addSubview(sub)
        }
    }
    
   public func removeFromSuperViewIfExist() {
        if base.superview != nil {
            base.removeFromSuperview()
        }
    }
}

public extension UIView  {
  
  // 用来判断该视图或者该视图的子视图中是否正在滑动
  // 应用场景: UIPickerView滑动的时候点击确定会导致数据不正确
  public var isScrolling: Bool {
    if let scrollView = self as? UIScrollView {
      if (scrollView.isDragging || scrollView.isDecelerating) { return true }
    }
    
    for subview in self.subviews {
      if ( subview.isScrolling ) { return true }
    }
    
    return false
    }
  
}
